package control;

import database.Database;
import java.sql.SQLException;
import model.*;

/**
 * Denne klasses opgave er at manipulere i databasen i forbindelse med kunde og kunde relationer
 * @author Sonny Sandberg
 */
public abstract class CustomerControl 
{
    /**
     * Tilføj Kunde
     * @param CustomerSpecified
     * @return boolean
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public static boolean addNewCustomer(CustomerSpecified customerSpecified) throws ClassNotFoundException, SQLException
    {
        boolean everythingDone = false;
        
        int customerAdded = addTheCustomer(customerSpecified.getCustomerName());
        if (customerAdded > 0)
        {
            customerSpecified.setCustomerName(model.handlers.CustomerHandler.getLastCustomerCreatedWithDetails(customerSpecified.getCustomerName()));
        }
        
        // Tjek først om telefonnummeret eksisterer.
        int phoneExist = model.handlers.TelephoneHandler.doesPhoneExist(customerSpecified.getCustomerTelephone());
        if (phoneExist == 0)
        {
            control.TelephoneControl.addPhone(customerSpecified.getCustomerTelephone());
        }
        
        // Hvis telefonen ikke eksisterede, så er den nu tilføjet
        // Hvis den eksisterede, ved vi, at det umuligt kan være med denne kunde, da en kunde her altid vil være nyoprettet.
        // Vi laver derfor en relation mellem kunden og telefonen
        if (phoneExist > 0 && customerSpecified.getCustomerName().getId() > 0)
        {
            control.TelephoneControl.addPhone(customerSpecified.getCustomerTelephone());
        }
        
        // Tjek om adresse eksisterer
        customerSpecified.setCustomerAddress(model.handlers.AddressHandler.getExistingAddress(customerSpecified.getCustomerAddress()));
        
        if (customerSpecified.getCustomerAddress().getId() > 0)
        {
            addCustomerAddressRelation(customerSpecified.getCustomerName(), customerSpecified.getCustomerAddress());
        }
        else
        {
            int addressAdded = control.AddressControl.addAddress(customerSpecified.getCustomerAddress());
            if (addressAdded > 0)
            {
                customerSpecified.setCustomerAddress(model.handlers.AddressHandler.getExistingAddress(customerSpecified.getCustomerAddress()));
                if (customerSpecified.getCustomerAddress().getId() > 0)
                {
                    addCustomerAddressRelation(customerSpecified.getCustomerName(), customerSpecified.getCustomerAddress());
                }
            }
        }
        
        everythingDone = true;
        
        return everythingDone;
    }
    
    /**
     * Tilføj selve kunden (CustomerName)
     * @param CustomerName
     * @return int
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    private static int addTheCustomer(CustomerName customer) throws ClassNotFoundException, SQLException
    {
        Database db = new Database();
        
        int mortician = 0;
        if (customer.isMortician() == true)
        {
            mortician = 1;
        }
        
        int added = db.manipulateDB("INSERT INTO bs_customer (name, mortician) VALUES ('"+customer.getName()+"', '"+mortician+"')");
        db.closeDB();
        return added;
    }
    
    /**
     * Tilføj kunde/adresse relation
     * @param CustomerName
     * @param Address
     * @return int
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    private static int addCustomerAddressRelation(CustomerName customer, Address address) throws ClassNotFoundException, SQLException
    {
        Database db = new Database();
        
        int added = db.manipulateDB("INSERT INTO bs_customer_address (fk_customer, fk_address) VALUES ('"+customer.getId()+"', '"+address.getId()+"')");
        db.closeDB();
        return added;
    }
}